home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / progjour / 1991 / 03 / misc.asm < prev    next >
Assembly Source File  |  1991-02-21  |  7KB  |  492 lines

  1.     title    miscellaneous
  2.     include    asm.inc
  3.  
  4.     public    abort_if_cf
  5. ;    public    move_file_pointer
  6. ;    public    read_from_file
  7.     public    strcmpi
  8.  
  9.     .data
  10.     extw    psp
  11.  
  12.     .const
  13. ertx_aborting    db    'Aborting',0
  14. ;ertx_disk_full    db    'Disk full',0
  15.  
  16.     .data?
  17. argc        dw    ?
  18. argv        dd    ?
  19.  
  20.  
  21.     .code
  22.  extn get_strerror,set_strerror,perror
  23.  extn ms_dos_strerror,malloc
  24.  
  25.  
  26. ;;    abort_if_cf
  27. ;
  28. abort_if_cf proc
  29.     jnc    aic2
  30.     call    get_strerror
  31.     jz    aic1
  32.     movx    si,NULL_POINTER
  33.     call    perror
  34. aic1:    lea    ax,ertx_aborting
  35.     call    set_strerror
  36.     call    perror
  37.     call    exit_with_error
  38. aic2:    ret
  39. abort_if_cf endp
  40.  
  41.  
  42. ;;    close file
  43. ;
  44. ;    entry    BX    handle
  45. ;    exit    Cf    0
  46. ;    uses    AX
  47. ;
  48. close_file proc
  49.     mov    ah,3Eh
  50.     jmp    ms_dos_strerror
  51. close_file endp
  52.  
  53.  
  54. ;;    err disk full
  55. ;
  56. ;    exit    Cf    1
  57. ;    uses    AX
  58. ;
  59. ;err_disk_full proc
  60. ;    lea    ax,ertx_disk_full
  61. ;    jmp    set_strerror
  62. ;err_disk_full endp
  63.  
  64.  
  65. ;;    exit program
  66. ;
  67. exit_program proc
  68.     mov    ax,4C00h
  69.     jmp    ms_dos
  70. exit_program endp
  71.  
  72.  
  73. ;;    exit with error
  74. ;
  75. exit_with_error proc
  76.     mov    ax,4C01h
  77.     jmp    ms_dos
  78. exit_with_error endp
  79.  
  80.  
  81. ;;    get vector
  82. ;
  83. ;    entry    AL    vector #
  84. ;    exit    DS:SI    selected vector
  85. ;        Zf    if null vector
  86. ;    uses    AX
  87. ;
  88. get_vector proc
  89.     mov    ah,0
  90.     add    ax,ax
  91.     add    ax,ax
  92.     xchg    ax,si
  93.     xor    ax,ax
  94.     mov    ds,ax
  95.     lds    si,[si]
  96.     mov    ax,ds
  97.     or    ax,si
  98.     ret
  99. get_vector endp
  100.  
  101.  
  102. ;;    input file size
  103. ;
  104. ;    entry    BX    file handle
  105. ;    exit    DX AX    size
  106. ;
  107. input_file_size proc
  108.     pushm    cx,di,si
  109.     mov    ax,4201h        ; get current position
  110.     movx    cx,0
  111.     movx    dx,0
  112.     call    ms_dos_strerror
  113.     jc    ifs1
  114.     mov    si,dx            ;  into SI DI
  115.     mov    di,ax
  116.  
  117.     mov    ax,4202h        ; move to end of file
  118.     movx    dx,0
  119.     call    ms_dos_strerror
  120.     jc    ifs1
  121.  
  122.     pushm    ax,dx
  123.     mov    ax,4200h        ; restore file position
  124.     mov    cx,si
  125.     mov    dx,di
  126.     call    ms_dos_strerror
  127.     popm    dx,ax
  128.  
  129. ifs1:    popm    si,di,cx
  130.     ret
  131. input_file_size endp
  132.  
  133.  
  134. ;;    move file pointer
  135. ;
  136. ;    entry    BX    file handle
  137. ;        DX AX    file position
  138. ;    exit    Cf    if error
  139. ;    uses    AX
  140. ;
  141. ;move_file_pointer proc
  142. ;    push    cx
  143. ;    mov    cx,dx
  144. ;    mov    dx,ax
  145. ;    mov    ax,4200h
  146. ;    call    ms_dos_dialog
  147. ;    pop    cx
  148. ;    ret
  149. ;move_file_pointer endp
  150.  
  151.  
  152. ;;    ms dos
  153. ;
  154. ms_dos    proc
  155.     int    21h
  156.     ret
  157. ms_dos    endp
  158.  
  159.  
  160. ;;    open input file
  161. ;
  162. ;    entry    DS:SI    string
  163. ;    exit    AX,BX    handle
  164. ;        Cf    if error, error text set
  165. ;    calls    offset_dos_error, ms_dos
  166. ;
  167. open_input_file proc
  168.     mov    ax,3D00h        ; (use 3D20 for shared access read)
  169.     xchg    dx,si
  170.     call    ms_dos_strerror
  171.     xchg    dx,si
  172.     mov    bx,ax
  173.     ret
  174. open_input_file endp
  175.  
  176.  
  177. ;;    read command line
  178. ;
  179. ;    exit    DS:SI    program command line
  180. ;
  181. read_command_line proc
  182.     mov    ds,psp[bp]
  183.     mov    si,81h
  184.     ret
  185. read_command_line endp
  186.  
  187.  
  188. ;;    read environment
  189. ;
  190. ;    exit    DS:SI    environment
  191. ;
  192. read_environment proc
  193.     xor    si,si
  194.     mov    ds,psp[bp]
  195.     mov    ds,[si+2Ch]
  196.     ret
  197. read_environment endp
  198.  
  199.  
  200. ;;    read from file
  201. ;
  202. ;    entry    BX    file handle
  203. ;        CX    byte count
  204. ;        ES:DI    destination
  205. ;    exit    AX    number of bytes read
  206. ;        Cf    if error
  207. ;
  208. ;read_from_file proc
  209. ;    push    ds
  210. ;    mov    ah,3Fh
  211. ;    push    es
  212. ;    pop    ds
  213. ;    xchg    dx,di
  214. ;    call    ms_dos_dialog
  215. ;    xchg    di,dx
  216. ;    pop    ds
  217. ;    ret
  218. ;read_from_file endp
  219.  
  220.  
  221. ;;    remove
  222. ;
  223. ;    entry    DS:SI    filename to delete
  224. ;    exit    Cf    if error
  225. ;    uses    AX
  226. ;
  227. remove    proc
  228.     mov    ah,41h            ; delete file
  229.     xchg    dx,si
  230.     call    ms_dos_strerror
  231.     xchg    dx,si
  232.     ret
  233. remove    endp
  234.  
  235.     even
  236. ;;    restore most
  237. ;
  238. ;    note    never call this routine
  239. ;
  240. restore_most proc
  241.     popm    bp,es,ds,si,di,dx,cx,bx
  242.     ret
  243. restore_most endp
  244.  
  245.     even
  246. ;;    save most
  247. ;
  248. ;    note    saves all registers except AX and BP.  however, the current
  249. ;        version also saves BP because the code works out that way.
  250. ;        the registers are automatically restored.  this routine is
  251. ;        called with a return address as the top of stack.
  252. ;
  253. save_most proc                ; +16 inner ret adr, +18 outer ret adr
  254.     push    cx            ; +14
  255.     push    dx            ; +12
  256.     push    di            ; +10
  257.     push    si            ; +8
  258.     push    ds            ; +6
  259.     push    es            ; +4
  260.     push    bp            ; +2
  261.     lea    bp,restore_most        ;     after execution of inner
  262.     push    bp            ; +0  routine, return to restore_most
  263.     mov    bp,sp
  264.     xchg    bx,[bp+16]        ;    bx above cx
  265.     push    bx            ; -2    setup return to inner routine
  266.     mov    bx,[bp+16]        ;    restore original BX and BP
  267.     mov    bp,[bp+2]
  268.     ret
  269. save_most endp
  270.  
  271.  
  272. ;;    set argc argv
  273. ;
  274. ;    exit    Cf    if no memory
  275. ;    uses    AX,CX,DI,SI,ES,DS
  276. ;
  277. set_argc_argv proc
  278.     mov    cx,256
  279.     call    malloc
  280.     jc    saa9            ; if no memory
  281.  
  282.     mov    argc[bp],1
  283.     mov    wptr argv[bp],di
  284.     mov    wptr argv[bp+2],es
  285.  
  286.     mov    ah,30h
  287.     call    ms_dos
  288.     cmp    al,3
  289.     jb    saa10            ;  if prior to DOS version 3.x
  290.  
  291.     call    read_environment    ; skip environment
  292. saa1:    call    strskp
  293.     lodsb
  294.     cmp    al,NULL_CHAR
  295.     jne    saa1
  296.     lodsw                ;  (skip 1)
  297.  
  298.     call    strcpy            ; copy program name which appears
  299.     inc    di            ;  after environment
  300.  
  301. saa2:    call    read_command_line
  302. saa3:    call    strskp_white
  303.     cmp    al,CR_CHAR
  304.     je    saa7            ; if no more arguments
  305.  
  306.     inc    argc[bp]
  307.     jmp    saa5
  308. saa4:    stosb
  309. saa5:    lodsb
  310.     cmp    al,SPACE_CHAR
  311.     jbe    saa6
  312.     cmp    al,','
  313.     je    saa6
  314.     cmp    al,'~'
  315.     jbe    saa4
  316.  
  317. saa6:    cmp    al,CR_CHAR
  318.     mov    al,NULL_CHAR
  319.     stosb
  320.     jne    saa3
  321.  
  322. saa7:    inc    di            ; word align table
  323.     and    di,-2
  324.  
  325.     lds    si,argv[bp]        ; get ptr to first argument
  326.     mov    wptr argv[bp],di    ; set pointer list offset
  327.     mov    cx,argc[bp]        ; get list count (cannot==0)
  328.  
  329. saa8:    mov    ax,si            ; build pointer list
  330.     stosw
  331.     call    strskp
  332.     loop    saa8
  333.     clc
  334. saa9:    ret
  335.  
  336. saa10:    mov    ax,'C'
  337.     stosw
  338.     jmp    saa2
  339. set_argc_argv endp
  340.  
  341.  
  342. ;;    stosb tmp path
  343. ;
  344. ;    entry    ES:DI    destination
  345. ;    exit    DI    updated
  346. ;    uses    AX
  347. ;
  348. stosb_tmp_path proc
  349.     mov    al,NULL_CHAR
  350.     stosb
  351.     dec    di
  352.     ret
  353. stosb_tmp_path endp
  354.  
  355.  
  356. ;;    strcmpi
  357. ;
  358. ;    entry    DS:SI    string1
  359. ;        ES:DI    string2
  360. ;    exit    Zf    if string1==string2
  361. ;    uses    AX
  362. ;
  363. strcmpi proc
  364.     pushm    di,si
  365. sci1:    mov    ah,es:[di]
  366.     lodsb
  367.     or    ax,ax
  368.     jz    sci2            ;  if same strings
  369.     inc    di
  370.     call    tolower
  371.     xchg    al,ah
  372.     call    tolower
  373.     cmp    ah,al
  374.     je    sci1            ;  if strings match so far
  375. sci2:    popm    si,di
  376.     ret
  377. strcmpi endp
  378.  
  379.  
  380. ;;    strcpy
  381. ;
  382. ;    entry    DS:SI    source ptr
  383. ;        ES:DI    destination ptr
  384. ;    exit    SI    updated past NULL
  385. ;        DI    updated, points to NULL
  386. ;    uses    AX
  387. ;
  388. strcpy    proc
  389.     lodsb
  390.     stosb
  391.     cmp    al,NULL_CHAR
  392.     jne    strcpy
  393.     dec    di
  394.     ret
  395. strcpy    endp
  396.  
  397.  
  398. ;;    strcpy limit
  399. ;
  400. ;    entry    DS:SI    source pointer
  401. ;        ES:DI    destination pointer
  402. ;        DX    destination limit
  403. ;    exit    DI    updated to last character in destination
  404. ;    uses    AX
  405. ;
  406. strcpy_limit proc
  407.     push    si
  408. scl1:    cmp    di,dx
  409.     jae    scl2            ; if at destination limit
  410.  
  411.     lodsb                ; else copy one character
  412.     stosb
  413.     cmp    al,0
  414.     jne    scl1
  415.  
  416.     dec    di
  417. scl2:    pop    si
  418.     ret
  419. strcpy_limit endp
  420.  
  421.  
  422. ;;    strlen
  423. ;
  424. ;    entry    DS:SI    string
  425. ;    exit    CX    byte count excluding NULL
  426. ;    uses    AX
  427. ;
  428. strlen    proc
  429.     mov    cx,si
  430. sln1:    lodsb
  431.     cmp    al,NULL_CHAR
  432.     jne    sln1
  433.     xchg    cx,si
  434.     sub    cx,si
  435.     dec    cx
  436.     ret
  437. strlen    endp
  438.  
  439.  
  440. ;;    strskp
  441. ;
  442. ;    entry    DS:SI    asciiz string ptr
  443. ;    exit    SI    updated past null
  444. ;    uses    AX
  445. ;
  446. strskp    proc
  447.     lodsb
  448.     cmp    al,NULL_CHAR
  449.     jne    strskp
  450.     ret
  451. strskp    endp
  452.  
  453.  
  454. ;;    strskp white
  455. ;
  456. ;    entry    DS:SI    text ptr
  457. ;    exit    SI    updated past spaces and tabs
  458. ;        AL    non-white character
  459. ;
  460. strskp_white proc
  461.     lodsb
  462.     cmp    al,SPACE_CHAR
  463.     je    strskp_white
  464.     cmp    al,TAB_CHAR
  465.     je    strskp_white
  466.     dec    si
  467.     ret
  468. strskp_white endp
  469.  
  470.  
  471. ;;    tolower
  472. ;
  473. ;    entry    AL    char
  474. ;    exit    AL    lower cased AL
  475. ;
  476. tolower proc
  477.     cmp    al,'A'
  478.     jb    tol1
  479.     cmp    al,'Z'
  480.     ja    tol1
  481.     or    al,20h
  482. tol1:    ret
  483. tolower endp
  484.  
  485.  
  486.     public    ms_dos,read_command_line,read_environment,set_argc_argv,strcpy
  487.     public    argv,argc,exit_program,exit_with_error,get_vector,tolower
  488.     public    close_file,open_input_file,input_file_size
  489.     public    remove,save_most,strlen,strcpy_limit,strskp
  490.  
  491.     end
  492.